#include "OpenGLWidgetEBO.h"
#include<QDebug>
// EBO 测试


static unsigned int VBO, VAO;
static unsigned int shaderProgram;
static unsigned int EBO;

static float vertices[] = {
0.5f, 0.5f, 0.0f,  // top right
0.5f,-0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};

static unsigned int indices[] = {
0,1,3, // first triangle
1,2,3 // second triangle 

};


static const char* vertexShaderSource = R"(
#version 330 core
layout(location=0) in vec3 aPos;
void main()
{
	gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
)";

static const char* fragmentShaderSource = R"(
out vec4 FragColor;
void main()
{
	FragColor = vec4(1.0f, 0.5f, 0.3f, 1.0f);
}
)";

OpenGLWidgetEBO::OpenGLWidgetEBO(QWidget* parent):QOpenGLWidget( parent)
{}

void OpenGLWidgetEBO::initializeGL()
{
	initializeOpenGLFunctions(); // 初始化opengl 相关的函数
	// 创建vao vbo 对象 ，并赋予ID

	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);

	// 创建VBO VAO 对象
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	// 为当前绑定到target 的缓冲区对象创建一个新的数据存储。
	// 如果data 不是NULL，则使用来自此指针的数据初始化数据存储
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// 创建EBO对象
	glGenBuffers(1, &EBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	// 告知显卡如何解析缓冲区里的属性值
	glVertexAttribPointer(0 ,3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),(void*)0);

	// 开启VAO 管理的第一个属性值
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glBindVertexArray(0);
	 

	// build and compile our shader program
	// 顶点着色器
	unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
	glCompileShader(vertexShader);

	//check for shader compile errors
	int success; char infoLog[512];
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
	if (!success)
	{
		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
		qDebug() << "error::shader::vertex::compileation_failed  " << infoLog;
	}
	// 片段着色器
	unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
	glCompileShader(fragmentShader);
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
	if (!success)
	{
		glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
		qDebug() << "error::shader::fragment::compileation_failed " << infoLog;
	}

	// link shaders
	shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glLinkProgram(shaderProgram);
	// check for linking errors
	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
	if (!success)
	{
		glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
		qDebug() << "error::shader::programe::linking_failed " << infoLog;
	}

	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);
}

void OpenGLWidgetEBO::resizeGL(int w, int h)
{
}

void OpenGLWidgetEBO::paintGL()
{
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	qDebug() << "paintGL";

	glUseProgram(shaderProgram);
	
	glBindVertexArray(VAO);

	glDrawElements(GL_TRIANGLES , 6 , GL_UNSIGNED_INT, 0);

	// 在传入 &indices 之前必须使用如下glBindBuffer 函数
	//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, &indices);
}
